/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import Merlion.SCM.bean.InventoryItemsBeanRemote;
import Merlion.SCM.bean.InventoryMovementsManagementBeanRemote;
import Merlion.util.state.InventoryState;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import org.primefaces.event.RowEditEvent;


@ManagedBean(name = "MaterialManagedBean")
@ViewScoped
public class MaterialManagedBean {

    private InventoryState newMaterial = new InventoryState();
    private InventoryState selectedMaterial;
    private InventoryState[] selectedMaterials;
    private InventoryState selectedViewMaterial;
    private List<InventoryState> materialList;
    private boolean editMode;
    private Integer year;
    private Integer week;
    private Integer month;
    private String remarks;
    private Date dateReceipt;
    @EJB
    private InventoryItemsBeanRemote iib;
    @EJB
    private InventoryMovementsManagementBeanRemote imm;

    /** Creates a new instance of MaterialManagedBean */
    public MaterialManagedBean() {
    }

    public void addMaterial(ActionEvent event) {
        System.out.println("Add Material");
        String name = newMaterial.getName();
        String desc = newMaterial.getDescription();
        int shelfLife = newMaterial.getShelfLife();
        int leadTime = newMaterial.getLeadTime();
        int lotSize = newMaterial.getLotSize();
        String lotSizeUnit = newMaterial.getLotSizeUnit();
        iib.createMaterial(name, desc, leadTime, shelfLife, lotSize, lotSizeUnit);

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("materials.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void deleteMaterial(ActionEvent event) {
        if (selectedMaterial != null) {
            System.out.println("Selected Material not null");
            Long id = selectedMaterial.getItemId();
            iib.deleteMaterial(id);
        }
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("materials.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void deleteMaterials() {
        System.out.println("Delete Materials");

        if (selectedMaterials != null) {
            System.out.println(":::WEB::: If selected Materials not null");
            for (int i = 0; i < selectedMaterials.length; i++) {
                System.out.println("selectedMaterials i: " + i + " - " + selectedMaterials[i].getItemId());
                iib.deleteMaterial(selectedMaterials[i].getItemId());
            }
        } else {
            System.out.println(":::WEB::: Selected Materials are null");
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("materials.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void editMaterial(ActionEvent event) {
        if (selectedViewMaterial != null) {
            Long id = selectedViewMaterial.getItemId();
            String name = selectedViewMaterial.getName();
            String desc = selectedViewMaterial.getDescription();
            int shelfLife = selectedViewMaterial.getShelfLife();
            int leadTime = selectedViewMaterial.getLeadTime();
            int lotSize = selectedViewMaterial.getLotSize();
            String lotSizeUnit = selectedViewMaterial.getLotSizeUnit();
            int inHandQty = selectedViewMaterial.getInHandQty();
            int reservedQty = selectedViewMaterial.getReservedQty();
            iib.updateMaterial(id, name, desc, inHandQty, reservedQty, leadTime, shelfLife, lotSize, lotSizeUnit);


            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("materials.xhtml");
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        }
    }

    public void onRowEditMaterial(RowEditEvent event) {

        System.out.println("On Row Edit Material");
        InventoryState eMaterial = (InventoryState) event.getObject();

        Long id = eMaterial.getItemId();
        String name = eMaterial.getName();
        String desc = eMaterial.getDescription();
        int shelfLife = eMaterial.getShelfLife();
        int leadTime = eMaterial.getLeadTime();
        int lotSize = eMaterial.getLotSize();
        String lotSizeUnit = eMaterial.getLotSizeUnit();
        int inHandQty = eMaterial.getInHandQty();
        int reservedQty = eMaterial.getReservedQty();

        iib.updateMaterial(id, name, desc, inHandQty, reservedQty, leadTime, shelfLife, lotSize, lotSizeUnit);
    }

    public InventoryState getNewMaterial() {
        return newMaterial;
    }

    public void setNewMaterial(InventoryState newMaterial) {
        this.newMaterial = newMaterial;
    }

    public List<InventoryState> getMaterialList() {
        if (materialList == null) {
            materialList = iib.viewMaterials();
        }
        return materialList;
    }

    public void setMaterialList(List<InventoryState> materialList) {
        this.materialList = materialList;
    }

    public InventoryState getSelectedMaterial() {
        return selectedMaterial;
    }

    public void setSelectedMaterial(InventoryState selectedMaterial) {
        this.selectedMaterial = selectedMaterial;
    }

    public InventoryState[] getSelectedMaterials() {
        return selectedMaterials;
    }

    public void setSelectedMaterials(InventoryState[] selectedMaterials) {
        this.selectedMaterials = selectedMaterials;
    }

    public InventoryState getSelectedViewMaterial() {
        return selectedViewMaterial;
    }

    public void setSelectedViewMaterial(InventoryState selectedViewMaterial) {
        this.selectedViewMaterial = selectedViewMaterial;
    }

    public void initView(PhaseEvent event) {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (materialList == null) {
            materialList = iib.viewMaterials();
        }
    }

    public boolean isEditMode() {
        return editMode;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    public Date getDateReceipt() {
        return dateReceipt;
    }

    public void setDateReceipt(Date dateReceipt) {
        this.dateReceipt = dateReceipt;
    }

    public Integer getMonth() {
        return month;
    }

    public void setMonth(Integer month) {
        this.month = month;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public Integer getWeek() {
        return week;
    }

    public void setWeek(Integer week) {
        this.week = week;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    public void executeIndividualWeeklyRequirements(ActionEvent event) {
        System.out.println("Execute weekly individual requirement");
        System.out.println("Year : " + year);
        System.out.println("Month : " + month);
        System.out.println("Week : " + week);
        System.out.println("Date : " + dateReceipt);
        System.out.println("Remarks : " + remarks);
        try {
            imm.executeWeeklyIndividualRequirement(year, month, week, dateReceipt, remarks);
            FacesContext context = FacesContext.getCurrentInstance();

            context.addMessage(null, new FacesMessage("Execution successful!", "Material requirement has been procured"));
        } catch (IllegalStateException e) {
            String msg = e.getMessage();
            System.out.println(msg);
            FacesContext context = FacesContext.getCurrentInstance();

            context.addMessage(null, new FacesMessage("Execution failed!", "The required material has been purchased previously."));

        }
    }
}
